<template>
  <view class="bill-manage-container">
    <!-- 搜索区域 -->
    <view class="search-section">
      <view class="search-card">
        <view class="search-row">
          <view class="search-item">
            <text class="search-label">姓名</text>
            <input class="search-input" v-model="searchForm.userName" placeholder="请输入姓名搜索" />
          </view>
        </view>
        <view class="search-buttons">
          <button class="search-btn" @click="handleSearch">搜索</button>
          <button class="reset-btn" @click="handleReset">重置</button>
        </view>
      </view>
    </view>

    <!-- 账单列表 -->
    <view class="bill-list-section">
      <view class="list-header">
        <text class="list-title">账单列表</text>
        <text class="list-count">当前 {{ billList.length }} 条</text>
      </view>
      
      <view v-if="loading" class="loading-container">
        <uni-load-more status="loading" />
      </view>
      
      <view v-else-if="billList.length === 0" class="empty-container">
        <view class="empty-icon">📄</view>
        <text class="empty-text">暂无账单数据</text>
      </view>
      
      <view v-else class="bill-list">
        <view v-for="bill in billList" :key="bill.id" class="bill-item" @click="viewBillDetail(bill)">
          <view class="bill-header">
            <view class="bill-info">
              <text class="bill-name">{{ bill.name }}</text>
              <text class="bill-phone">{{ maskPhone(bill.phone) }}</text>
            </view>
            <view class="bill-amount">
              <text class="amount-value">¥{{ bill.totalPrice || 0 }}</text>
              <text class="bill-type">{{ getTypeLabel(bill.type) }}</text>
            </view>
          </view>
          <view class="bill-details">
            <view class="detail-row">
              <text class="detail-label">订单ID:</text>
              <text class="detail-value">{{ bill.orderId }}</text>
            </view>
            <view class="detail-row">
              <text class="detail-label">购买数量:</text>
              <text class="detail-value">{{ bill.buyCount }}个月</text>
            </view>
            <view class="detail-row">
              <text class="detail-label">单价:</text>
              <text class="detail-value">¥{{ bill.unitPrice || 0 }}/月</text>
            </view>
            <view class="detail-row">
              <text class="detail-label">创建时间:</text>
              <text class="detail-value">{{ formatTime(bill.createTime) }}</text>
            </view>
          </view>
          <view class="bill-footer" v-if="bill.isPolice === '1'">
            <view class="police-tag">公安</view>
          </view>
        </view>
      </view>
    </view>

    <!-- 上拉加载更多 -->
    <view class="load-more-section" v-if="billList.length > 0 && !loading">
      <uni-load-more 
        :status="loadMoreStatus" 
        @clickLoadMore="loadMoreData"
      />
    </view>
  </view>
</template>

<script>
import { getBillList } from '@/api/login'

export default {
  data() {
    return {
      searchForm: {
        userName: ''
      },
      billList: [],
      loading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        hasMore: true
      },
      loadMoreStatus: 'more' // more、loading、noMore
    }
  },
  onLoad() {
    this.loadBillList()
  },
  onPullDownRefresh() {
    this.refreshData()
  },
  onReachBottom() {
    this.loadMoreData()
  },
  methods: {
    async loadBillList(isLoadMore = false) {
      if (this.loading) return
      
      this.loading = true
      if (!isLoadMore) {
        this.loadMoreStatus = 'loading'
      }
      
      try {
        const params = {
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize
        }
        
        // 添加姓名搜索条件
        if (this.searchForm.userName) {
          params.landlordName = this.searchForm.userName.trim()
        }
        
        const res = await getBillList(params)
        if (res.code === 200) {
          const newData = res.rows || []
          
          if (isLoadMore) {
            // 上拉加载更多，追加数据
            this.billList = [...this.billList, ...newData]
          } else {
            // 下拉刷新或首次加载，替换数据
            this.billList = newData
          }
          
          // 判断是否还有更多数据
          if (newData.length < this.pagination.pageSize) {
            this.pagination.hasMore = false
            this.loadMoreStatus = 'noMore'
          } else {
            this.pagination.hasMore = true
            this.loadMoreStatus = 'more'
          }
        } else {
          uni.showToast({ title: res.msg || '获取账单列表失败', icon: 'none' })
        }
      } catch (error) {
        console.error('获取账单列表异常:', error)
        uni.showToast({ title: '网络异常，请稍后重试', icon: 'none' })
      } finally {
        this.loading = false
        if (!isLoadMore) {
          uni.stopPullDownRefresh()
        }
      }
    },
    
    // 刷新数据
    refreshData() {
      this.pagination.current = 1
      this.pagination.hasMore = true
      this.loadMoreStatus = 'more'
      this.loadBillList(false)
    },
    
    // 加载更多数据
    loadMoreData() {
      if (!this.pagination.hasMore || this.loading) {
        return
      }
      
      this.pagination.current += 1
      this.loadMoreStatus = 'loading'
      this.loadBillList(true)
    },
    
    handleSearch() {
      this.refreshData()
    },
    
    handleReset() {
      this.searchForm.userName = ''
      this.refreshData()
    },
    
    viewBillDetail(bill) {
      // 显示账单详情
      uni.showModal({
        title: '账单详情',
        content: `订单ID: ${bill.orderId}\n姓名: ${bill.name}\n手机: ${bill.phone}\n金额: ¥${bill.totalPrice}\n时间: ${this.formatTime(bill.createTime)}`,
        showCancel: false
      })
    },
    
    maskPhone(phone) {
      if (!phone) return ''
      return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
    },
    
    formatTime(time) {
      if (!time) return ''
      return new Date(time).toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      })
    },
    
    getTypeLabel(type) {
      const typeMap = {
        '1': '运维费',
        '2': '门禁卡',
        '3': '门禁缴费'
      }
      return typeMap[type] || '未知'
    }
  }
}
</script>

<style lang="scss" scoped>
.bill-manage-container {
  min-height: 100vh;
  background: #f5f7fa;
  padding: 24rpx;
}

/* 搜索区域 */
.search-section {
  margin-bottom: 24rpx;
}

.search-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 32rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
}

.search-row {
  margin-bottom: 24rpx;
}

.search-item {
  display: flex;
  flex-direction: column;
}

.search-label {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 12rpx;
}

.search-input {
  height: 72rpx;
  border: 1px solid #e5e5e5;
  border-radius: 8rpx;
  padding: 0 16rpx;
  font-size: 28rpx;
  background: #fafafa;
}

.search-buttons {
  display: flex;
  gap: 24rpx;
  margin-top: 12rpx;
}

.search-btn {
  flex: 1;
  height: 72rpx;
  background: #007AFF;
  color: #fff;
  border: none;
  border-radius: 8rpx;
  font-size: 28rpx;
}

.reset-btn {
  flex: 1;
  height: 72rpx;
  background: #f5f5f5;
  color: #666;
  border: none;
  border-radius: 8rpx;
  font-size: 28rpx;
}

/* 列表区域 */
.bill-list-section {
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-bottom: 24rpx;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 32rpx;
  border-bottom: 1px solid #f5f5f5;
}

.list-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.list-count {
  font-size: 24rpx;
  color: #666;
}

.loading-container,
.empty-container {
  padding: 80rpx 0;
  text-align: center;
}

.empty-icon {
  font-size: 80rpx;
  margin-bottom: 24rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

/* 账单项 */
.bill-item {
  padding: 32rpx;
  border-bottom: 1px solid #f5f5f5;
  transition: background 0.2s;
}

.bill-item:active {
  background: #f8f9fa;
}

.bill-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
}

.bill-info {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.bill-name {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
}

.bill-phone {
  font-size: 24rpx;
  color: #666;
}

.bill-amount {
  text-align: right;
  display: flex;
  flex-direction: column;
  gap: 4rpx;
}

.amount-value {
  font-size: 32rpx;
  font-weight: bold;
  color: #007AFF;
}

.bill-type {
  font-size: 22rpx;
  color: #666;
}

.bill-details {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
  margin-bottom: 16rpx;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail-label {
  font-size: 24rpx;
  color: #666;
}

.detail-value {
  font-size: 24rpx;
  color: #333;
}

.bill-footer {
  display: flex;
  justify-content: flex-end;
}

.police-tag {
  background: #ff6b6b;
  color: #fff;
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  font-size: 20rpx;
}

/* 加载更多 */
.load-more-section {
  margin-top: 24rpx;
  padding: 24rpx 0;
  text-align: center;
}
</style> 